package com.mq.dpca.msg;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;
import com.mq.dpca.util.UuidUtil;

/**
 * MQ�ַ��͹���
 * 
 */
public class MsgSend {

	MQQueueConnectionFactory mcf;
	QueueConnection qconn;
	MQConfig mc = null;

	/**
	 * ��ʼ������
	 * 
	 * @throws JMSException
	 */
	public void openConnection() throws JMSException {
//		mc = MQConfig.getInstance();
		mcf = new MQQueueConnectionFactory();
		mcf.setConnectionNameList("10.7.101.209");
		mcf.setHostName("10.7.101.209");
		mcf.setChannel("SYSTEM.ADMIN.SVRCONN");
		mcf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
		mcf.setClientReconnectOptions(WMQConstants.WMQ_CLIENT_RECONNECT);
		mcf.setPort(1414);
		mcf.setCCSID(1208);
		mcf.setQueueManager("SYS_ESBIN");
		qconn = mcf.createQueueConnection();
	}

	/**
	 * ������Ϣ
	 * 
	 * @param 服务名称
	 * @param xml内容
	 * @throws JMSException
	 * @throws InterruptedException
	 */
	public void send(String key, String msgInfo) {
		String uid = UuidUtil.getUUID();
		QueueSession session = null;
		QueueSender sender = null;
		try {
			openConnection();
			session = qconn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			Queue queue = session.createQueue(mc.getMQ_QUEUE_NAME());
			sender = session.createSender(queue);
			TextMessage msg = session.createTextMessage();
			msg.setText(msgInfo);
//			msg.setObjectProperty("requestId", uid);名称	值
			msg.setStringProperty("ESBHeader", "{\"RequestID\":\"MQ_TA_BENEFITINFO20180726164846611\",\"SourceSystem\":\"TA\",\"TargetSystem\":\"ESB\",\"ServiceName\":\"MQ_TA_BENEFITINFO\",\"ServiceOperation\":\"\",\"ServiceVersion\":\"1.0\"}");
			sender.send(msg);
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			try {
				sender.close();
				session.close();
				qconn.close();
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * ��������
	 * @param args
	 * @throws JMSException
	 * @throws InterruptedException
	 */

	public static void main(String[] args) throws JMSException,
			InterruptedException {
		MsgSend ms = new MsgSend();
		String str = "[{\"indiminaccountunit\":0,\"indioncehighestscale\":0,\"instoncehighestscale\":0,\"redemptionclose\":0,\"lifecycle\":0,\"lifecycleunit\":\"0\",\"contractnumflag\":\"1\",\"maxinvestors\":50,\"ipocalcflag\":\"1\",\"ipointmode\":\"1\",\"instunitappamount\":0,\"fundtype\":\"061\",\"endinterestdate\":\"20171018\",\"instmaxsubamount2\":0,\"issuedate\":\"20171018\",\"maxcustsize\":0,\"enddate\":\"20181017\",\"substype\":\"0\",\"benefitlevel\":\"1\",\"facevalue\":1,\"benefitrate\":\"0\",\"minsize\":0,\"amount300\":3000000,\"sharetypes\":\"A\",\"instminsubamount2\":0,\"indihighestscale\":0,\"instminsubamount\":0,\"instminaccountunit\":0,\"indiunitsubamount\":0,\"instappsubsam";
		ms.send("service1", str);
		// Thread.sleep(10000);
		System.out.print("");
	}
}
